package com.dmap.admin.controller.backgroundv3;

import com.dmap.admin.constants.VersionConstant;
import com.dmap.admin.controller.backgroundv3.param.IosVersionEditParam;
import com.dmap.admin.controller.backgroundv3.param.IosVersionParam;
import com.dmap.admin.controller.backgroundv3.param.IosVersionPublishParam;
import com.dmap.admin.po.IosVersion;
import com.dmap.admin.service.backgroundv3.IOSVersionService;
import com.dmap.admin.vo.backgroundv3.IosVersionInfoVo;
import com.dmap.admin.vo.backgroundv3.IosVersionSelectVo;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zmh
 * @create: 18-8-22
 * @description: ios版本Controller
 **/
@Api(value = "IOS版本", description = "background/IOS版本")
@RestController
@RequestMapping(value = "/background/dmap-knowledge-server/v1/iosVersion/")
public class IosVersionBackController {

    @Resource
    private IOSVersionService iosVersionService;

    @ResponseBody
    @ApiOperation(value = "新增IOS版本", notes = "新增IOS版本")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseVo insertIosVErsion(@RequestBody IosVersionParam param) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(param.getAppNumber()) || StringUtils.isBlank(param.getDescription()) || StringUtils.isBlank(param.getDescriptionEn()) ||
                StringUtils.isBlank(param.getMd5()) || StringUtils.isBlank(param.getMinVersion()) || StringUtils.isBlank(param.getNewVersion()) ||
                param.getStatus() == 0 || StringUtils.isBlank(param.getUrl())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = iosVersionService.insertIosVersion(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "修改IOS版本", notes = "修改IOS版本")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseVo updateIosVErsion(@RequestBody IosVersionEditParam param) {
        BaseVo result = new BaseVo();
        if (!StringUtils.isNumeric(param.getIosVersionId()) || StringUtils.isBlank(param.getIosVersionId()) || StringUtils.isBlank(param.getAppNumber()) || StringUtils.isBlank(param.getDescription()) || StringUtils.isBlank(param.getDescriptionEn()) ||
                StringUtils.isBlank(param.getMd5()) || StringUtils.isBlank(param.getMinVersion()) || StringUtils.isBlank(param.getNewVersion()) ||
                param.getStatus() == 0 || StringUtils.isBlank(param.getUrl())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = iosVersionService.updateIosVersion(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        if (success == 2) {
            result.setCode(VersionConstant.RESPONSE_IOS_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_IOS_VERSION_EXIST_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "发布IOS版本", notes = "发布IOS版本")
    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    public BaseVo updateIosVErsionPublish(@RequestBody IosVersionPublishParam param) {
        BaseVo result = new BaseVo();
        if (!StringUtils.isNumeric(param.getIosVersionId()) || StringUtils.isBlank(param.getIosVersionId())
                || !StringUtils.isNumeric(param.getPromulgatorId()) || StringUtils.isBlank(param.getPromulgatorId())
        ) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = iosVersionService.updateIosVersionPublish(param);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        if (success == 2) {
            result.setCode(VersionConstant.RESPONSE_IOS_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_IOS_VERSION_EXIST_ERROR_MSG);
        }
        return result;
    }

    @Transactional
    @ResponseBody
    @ApiOperation(value = "删除IOS版本", notes = "删除IOS版本")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public BaseVo deleteIosVErsion(@RequestParam String iosVersionIds) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(iosVersionIds)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = iosVersionService.deleteIosVersionList(iosVersionIds);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);

        }
        return result;

    }

    @ResponseBody
    @ApiOperation(value = "查询IOS版本", notes = "查询IOS版本")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public BaseVo selectIosVErsion(@RequestParam(required = false) String companyNumber,
                                   @RequestParam(required = false) String appNumber,
                                   @RequestParam String lang,
                                   @RequestParam(required = false) String columnId,
                                   @RequestParam(required = false) String key,
                                   @RequestParam(required = false, defaultValue = "0") long order,
                                   @RequestParam int pageIndex,
                                   @RequestParam int pageSize) {
        BaseVo result = new BaseVo();
//        if (lang == null || pageIndex == 0 || pageSize == 0) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (appNumber != null && StringUtils.isBlank(appNumber)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (companyNumber != null && StringUtils.isBlank(companyNumber)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (columnId != null && !StringUtils.isNumeric(columnId)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        if (StringUtils.isBlank(lang)) {
//            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
//            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
//            return result;
//        }
//        ArticleBackSelectAll articleBackSelectAll = new ArticleBackSelectAll();
//        articleBackSelectAll.setArticleList(articleExtensionService.selectArticleList(companyNumber, appNumber, lang, columnId, key, order, pageIndex, pageSize));
//        articleBackSelectAll.setCount(articleExtensionService.countArticleList(companyNumber, appNumber, lang, columnId, key, order));
//        result.setData(articleBackSelectAll);
        List<IosVersion> iosVersionList = iosVersionService.selectIosVersion();
        List<IosVersionSelectVo> iosVersionSelectVoList = new ArrayList<>();
        for(IosVersion iosVersion : iosVersionList){
            IosVersionSelectVo iosVersionSelectVo = new IosVersionSelectVo(iosVersion);
            iosVersionSelectVoList.add(iosVersionSelectVo);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查看IOS版本信息", notes = "查看IOS版本信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public BaseVo selectIosVersion(@RequestParam String iosVersionId) {
        BaseVo result = new BaseVo();
        if(StringUtils.isBlank(iosVersionId) || !StringUtils.isNumeric(iosVersionId)){
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        IosVersion iosVersion = iosVersionService.getIosVersion(Long.parseLong(iosVersionId));
        if (iosVersion == null) {
            result.setCode(VersionConstant.RESPONSE_IOS_VERSION_EXIST_ERROR_CODE);
            result.setMsg(VersionConstant.RESPONSE_IOS_VERSION_EXIST_ERROR_MSG);
        }
        IosVersionInfoVo iosVersionInfoVo = new IosVersionInfoVo(iosVersion);
        result.setData(iosVersionInfoVo);
        return result;
    }
}
